﻿using Gds.ViewModel.pageViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Gas_Server_framework.PageView
{
    /// <summary>
    /// ViewShowLog.xaml 的交互逻辑
    /// </summary>
    public partial class ViewShowLog : Window
    {
        public ViewShowLog()
        {
            InitializeComponent();
            border_log.DataContext = CommunicationLogViewModel.Instance;
            wrap_box.DataContext = MainViewModel.Instance;
            border_realTimeData.DataContext = CommunicationLogViewModel.Instance;
            border_runState.DataContext = CommunicationLogViewModel.Instance;
            init();
        }
        private void init()
        {
            //加载总线列表
            var listTotal = CommunicationLogViewModel.Instance.FindTotalLineList();
            CommunicationLogViewModel.Instance.CurrentRunStateModel = new Gds.Model.TestFunctionModel.TaskRunStateModel();
            addComboxItem(cmb_totalLineList, listTotal);
            if (CommunicationLogViewModel.Instance.GasMeterRealTimeDatas == null)
            {
                CommunicationLogViewModel.Instance.GasMeterRealTimeDatas = new Gds.Model.AsyncObservableCollection<Gds.Model.ConfigModel.GasMeter.ShowRealTimeModel>();

            }
            CommunicationLogViewModel.Instance.GasMeterRealTimeDatas = new Gds.Model.AsyncObservableCollection<Gds.Model.ConfigModel.GasMeter.ShowRealTimeModel>();

        }
        private void cmb_totalLineList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmb_totalLineList.SelectedValue != null)
            {
                var listController = CommunicationLogViewModel.Instance.FindControllerAddressList(int.Parse(cmb_totalLineList.SelectedValue.ToString()));
                addComboxItem(cmb_AddressList, listController);
                MainViewModel.Instance.ShowLogControllerTotalLine = int.Parse(cmb_totalLineList.SelectedValue.ToString());
                CommunicationLogViewModel.Instance.GasMeterRealTimeDatas = new Gds.Model.AsyncObservableCollection<Gds.Model.ConfigModel.GasMeter.ShowRealTimeModel>();
            }


        }

        private void cmb_AddressList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmb_totalLineList.SelectedValue != null&& cmb_AddressList.SelectedValue!=null)
            {
                if (CommunicationLogViewModel.Instance.CommunicationLogs != null)
                { 
               CommunicationLogViewModel.Instance.CommunicationLogs.Clear();

                }
                MainViewModel.Instance.ShowLogControllerAddressIndex = int.Parse(cmb_AddressList.SelectedValue.ToString());
                CommunicationLogViewModel.Instance.GasMeterRealTimeDatas = new Gds.Model.AsyncObservableCollection<Gds.Model.ConfigModel.GasMeter.ShowRealTimeModel>();
            }
        }
        private void addComboxItem(ComboBox _combox, List<string> _items)
        {
            _combox.Items.Clear();
            foreach (string item in _items)
            {
                _combox.Items.Add(item);
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainViewModel.Instance.ShowLogControllerAddressIndex = 0;
            MainViewModel.Instance.ShowLogControllerTotalLine = 0;
            MainViewModel.Instance.IsShowCommunication = false;
            MainViewModel.Instance.IsSaveLog = false;
        }
    }
}
